﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ATM
{
    public partial class thongtinbank : Form
    {
        private SqlConnection conn;
        private DataTable dt = new DataTable("TB_BANK_INFO");
        private SqlDataAdapter da = new SqlDataAdapter();

        private void connect()
        {

            String cn = @"Data Source=(local);Initial Catalog=ATM_PM1;Integrated Security=True";
            try
            {
                conn = new SqlConnection(cn);
                conn.Open();

            }
            catch (SqlException ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("khong thanh cong");
            }
        }

        private void disconnect()
        {
            conn.Close();
            conn.Dispose();
            conn = null;
        }
        public thongtinbank()
        {
            InitializeComponent();
        }

        private void thongtinbank_Load(object sender, EventArgs e)
        {
            connect();
            getdata();
            binding();
        }
        private void getdata()
        {
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = CommandType.Text;
            command.CommandText = @"select  * from TB_BANK_INFO ";
            da.SelectCommand = command;
            da.Fill(dt);
            shownganhang.DataSource = dt;
        }
        private void binding()
        {
            txtten.DataBindings.Clear();
            txtten.DataBindings.Add("text",shownganhang.DataSource,"Bank_Name");
            txtdiachi.DataBindings.Clear();
            txtdiachi.DataBindings.Add("text", shownganhang.DataSource, "Bank_Address");
            txtphone.DataBindings.Clear();
            txtphone.DataBindings.Add("text", shownganhang.DataSource, "Bank_Phone");
            txtfax.DataBindings.Clear();
            txtfax.DataBindings.Add("text", shownganhang.DataSource, "Bank_Fax");
            txtid.DataBindings.Clear();
            txtid.DataBindings.Add("text", shownganhang.DataSource, "Bank_ID");
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            DataRow row = dt.Select("Bank_ID = " + txtid.Text)[0];
            row.BeginEdit();
            row["Bank_ID"] = txtid.Text;
            row["Bank_Name"] = txtten.Text;
            row["Bank_Address"] = txtdiachi.Text;
            row["Bank_Phone"] = txtphone.Text;
            row["Bank_Fax"] = txtfax.Text;
            SqlCommand commandUpdate = new SqlCommand();
            commandUpdate.Connection = conn;
            commandUpdate.CommandType = CommandType.Text;
            commandUpdate.CommandText = @"Update TB_BANK_INFO set Bank_Name=@Bank_Name,Bank_Address=@Bank_Address,Bank_Phone=@Bank_Phone,Bank_Fax=@Bank_Fax ";
            commandUpdate.Parameters.Add("@Bank_Name", SqlDbType.NVarChar, 30, "Bank_Name");
            commandUpdate.Parameters.Add("@Bank_Address", SqlDbType.NVarChar, 30, "Bank_Address");
            commandUpdate.Parameters.Add("@Bank_Phone", SqlDbType.NVarChar, 30, "Bank_Phone");
            commandUpdate.Parameters.Add("@Bank_Fax", SqlDbType.NVarChar, 30, "Bank_Fax");
            da.UpdateCommand = commandUpdate;
            da.Update(dt);
            MessageBox.Show("Bạn đã sửa thành công !", "Thông báo", MessageBoxButtons.OK);
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Close();
            Dispose();
        }
    }
}
